package controller;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;

@WebServlet("/empmain")
public class MainController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //响应头
        response.setStatus(500,"没错硬写错");
        //响应行
        response.setContentType("text/html;charset=utf-8");
        //response.setHeader("ContentType","text/html;charset=utf-8");
        //response.setHeader("className","dz18");
        //响应主体
        response.getWriter().println("<a>我是响应主体</a>");
    }

    public void showRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获得请求地址
        //1.获得请求方式
        String method = request.getMethod();
        System.out.println("请求方式"+method);
        //2.请求地址
        System.out.println("uri:"+request.getRequestURL());
        System.out.println("uri:"+request.getRequestURI());
        //3.获得协议
        System.out.println("协议名称:"+request.getScheme());
        //4.版本
        System.out.println("版本号："+request.getProtocol());
        //5.访问项目名
        System.out.println("访问项目名"+request.getContextPath());


        System.out.println("----------------------------------------------");


        //获得请求行信息
        System.out.println("请求行Accept:"+request.getHeader("Accept")) ;

        Enumeration<String> headNames = request.getHeaderNames();
        while (headNames.hasMoreElements()){
            String string  = headNames.nextElement();
            System.out.println("请求行："+request.getHeader(string));
        }

        System.out.println("----------------------------------------------");

        //获得请求主体的信息

        request.setCharacterEncoding("utf-8");//请求设置不乱码
        //response.setContentType("utf-8");//响应设置不乱码

        String empno = request.getParameter("empno");
        //null 和 “ ” 的区别   null 写错了，没对上   “” 没东西
        String ename = request.getParameter("ename");
        String sex = request.getParameter("sex");

        String[] citys = request.getParameterValues("citys");
        String year = request.getParameter("year");

        System.out.println("员工编号："+empno+"；员工姓名"+ename+"；性别"+sex+"；出生年份"+year+"；城市"+ Arrays.toString(citys) );

    }
}
